#pragma once

#ifndef CLIB_STACK_SOCK_H
#define CLIB_STACK_SOCK_H

#include <sys/ioctl.h>
#include <errno.h>
#include "clib_sock.h"

#ifdef __cplusplus
extern "C" {
#endif

int32_t 
clib_getsockopt(clib_sock_t* sock,
                int32_t sockid,
                int32_t level,
                int32_t optname,
                void *optval,
                socklen_t *optlen);


int32_t 
clib_setsockopt(clib_sock_t* sock,
                int32_t sockid,
                int32_t level,
		        int32_t optname,
                const void *optval,
                socklen_t optlen);


int32_t 
clib_setsock_nonblock(clib_sock_t* sock, int32_t sockid);

/* mtcp_socket_ioctl: similar to ioctl, 
   but only FIONREAD is supported currently */
int32_t 
clib_socket_ioctl(clib_sock_t* sock, int32_t sockid, int32_t request, void *argp);


int32_t 
clib_socket(clib_sock_t* sock, int32_t domain, int32_t type, int32_t protocol);


int32_t 
clib_bind(clib_sock_t* sock, 
          int32_t sockid, 
		  const struct sockaddr *addr,
          socklen_t addrlen);


int32_t 
clib_listen(clib_sock_t* sock, int32_t sockid, int32_t backlog);

int32_t 
clib_accept(clib_sock_t* sock, int32_t sockid, struct sockaddr *addr, socklen_t *addrlen);

// int32_t 
// clib_init_rss(mctx_t mctx, in_addr_t saddr_base, int32_t num_addr, 
// 		in_addr_t daddr, in_addr_t dport);

int32_t 
clib_connect(clib_sock_t* sock, 
             int32_t sockid, 
		     const struct sockaddr *addr,
             socklen_t addrlen);


int32_t 
clib_close(clib_sock_t* sock, int32_t sockid);


int32_t
clib_getsockname(clib_sock_t* sock,
                 int32_t sockid,
                 struct sockaddr *addr,
                 socklen_t *addrlen);


int32_t
clib_getpeername(clib_sock_t* sock,
                 int32_t sockid,
                 struct sockaddr *addr,
		         socklen_t *addrlen);


inline ssize_t
clib_read(clib_sock_t* sock, int32_t sockid, char *buf, size_t len);


ssize_t
clib_recv(clib_sock_t* sock, int32_t sockid, char *buf, size_t len, int32_t flags);

ssize_t
clib_write(clib_sock_t* sock, int32_t sockid, const char *buf, size_t len);

#ifdef __cplusplus
}
#endif

#endif